﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using TSL.Common.DEncrypt;

namespace TSL.Common
{
    public class PassWord
    {
        public static string deBankPW(string password)
        {
            try
            {
                int BankPassWordMode = int.Parse(ConfigurationManager.AppSettings["BankPassWordMode"].ToString());

                if (BankPassWordMode == 0)
                {
                    return DESEncrypt.Encrypt(password);
                }

                if (BankPassWordMode == 1)
                {
                    return md5(password, 16);
                }

                if (BankPassWordMode == 2)
                {
                    return md5(password, 32);
                }
                return password;

            }
            catch
            {
            }
            return "";
        }



        public static string unBankPW(string password)
        {
            try
            {
                int BankPassWordMode = int.Parse(ConfigurationManager.AppSettings["BankPassWordMode"].ToString());

                if (BankPassWordMode == 0)
                {
                    return DESEncrypt.Decrypt(password);
                }

                return password;
            }
            catch
            {
            }
            return "";
        }

        public static string deUserPW(string password,int mode)
        {
            if (mode == 0)
            {
                return DESEncrypt.Encrypt(password);
            }

            if (mode == 1)
            {
                return md5(password, 16);
            }

            if (mode == 2)
            {
                return md5(password, 32);
            }
            return password;
        }

        public static string deUserPW(string password)
        {
            try
            {
                int PassWordMode = int.Parse(ConfigurationManager.AppSettings["PassWordMode"].ToString());
                return deUserPW(password, PassWordMode);
            }
            catch
            {
            }
            return "";
        }


        public static string unUserPW(string password)
        {
            try
            {
                int PassWordMode = int.Parse(ConfigurationManager.AppSettings["PassWordMode"].ToString());

                if (PassWordMode == 0)
                {
                    return DESEncrypt.Decrypt(password);
                }

                return password;
            }
            catch
            {
            }
            return "";
        }

        private static string md5(string str, int code)
        {
            if (code == 16) //16位MD5加密（取32位加密的9~25字符） 
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
            }
            else//32位加密 
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
            }
        }
    }
}
